package org.jsola.contract.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.contract.constant.ContractType;
import org.jsola.contract.constant.FileType;
import org.jsola.contract.constant.SealType;
import org.jsola.contract.dto.ContractAddDTO;
import org.jsola.contract.dto.ContractUpdateDTO;
import org.jsola.contract.entity.ContractDO;
import org.jsola.contract.query.ContractQuery;
import org.jsola.contract.service.IContractService;
import org.jsola.contract.vo.ContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 合同
 *
 * @author JanusMix
 */
@Component
public class ContractTestHelper extends BaseTestHelper {
    
    @Autowired
    private CategoryTestHelper categoryTestHelper;
    @Autowired
    private IContractService contractService;

    /**
     * 生成add测试数据
     * @return ContractAddDTO
     */
    public ContractAddDTO getContractAddDTO(){
        ContractAddDTO contractAddDTO = new ContractAddDTO();
        contractAddDTO.setCategoryId(categoryTestHelper.saveCategoryExample().getId());
        contractAddDTO.setTemplateId(SafeKit.getLong(getRandomValue("bigint",11)));
        contractAddDTO.setContractNo(getRandomValue("varchar",255));
        contractAddDTO.setSignNo(getRandomValue("varchar",20));
        contractAddDTO.setSubjectId(getRandomValue("varchar",32));
        contractAddDTO.setName(getRandomValue("varchar",255));
        contractAddDTO.setSealType(SealType.SINGLE.getValue());
        contractAddDTO.setContractType(ContractType.ONLINE.getValue());
        contractAddDTO.setPublishDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        contractAddDTO.setSignDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        contractAddDTO.setEndDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        contractAddDTO.setEmpSignDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        contractAddDTO.setSignFinishDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        contractAddDTO.setPartyAAccountId(getRandomValue("varchar",40));
        contractAddDTO.setPartyAName(getRandomValue("text",2147483647));
        contractAddDTO.setPartyBAccountId(getRandomValue("varchar",40));
        contractAddDTO.setPartyBName(getRandomValue("text",2147483647));
        contractAddDTO.setPartyBPhone(getRandomValue("varchar",50));
        contractAddDTO.setPrice(SafeKit.getLong(getRandomValue("bigint",11)));
        contractAddDTO.setDateline(SafeKit.getInteger(getRandomValue("int",11)));
        contractAddDTO.setFileType(FileType.LABORCONTRACTLIMIT.getValue());
        contractAddDTO.setContent(getRandomValue("longtext",2147483647));
        contractAddDTO.setNeedInitAgain(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        contractAddDTO.setAgainContractId(SafeKit.getLong(getRandomValue("bigint",11)));
        contractAddDTO.setAppIsShow(SafeKit.getBoolean(getRandomValue("tinyint",0)));
        contractAddDTO.setIsCheckFace(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        contractAddDTO.setIsCheckSms(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        contractAddDTO.setIsView(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        return contractAddDTO;
    }


    /**
     * 生成update测试数据
     * @return ContractUpdateDTO
     */
    public ContractUpdateDTO getContractUpdateDTO(ContractVO contractVO){
        return contractService.selectDOById(contractVO.getId(), SITE_ID).to(ContractUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return ContractQuery
     */
    public ContractQuery getContractQuery() {
        ContractQuery contractQuery = new ContractQuery();
        List<ContractDO> contractDOList = contractService.list(new ContractDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(contractDOList)) {
            ContractDO contractDO = contractDOList.get(0);
            contractQuery = contractDO.to(ContractQuery.class);
            if (contractDO.getPublishDate() != null) {
                contractQuery.setPublishDateStart(DateKit.getBeforeDay(contractDO.getPublishDate(),-1));
                contractQuery.setPublishDateEnd(DateKit.getBeforeDay(contractDO.getPublishDate(),1));
            }
            if (contractDO.getSignDate() != null) {
                contractQuery.setSignDateStart(DateKit.getBeforeDay(contractDO.getSignDate(),-1));
                contractQuery.setSignDateEnd(DateKit.getBeforeDay(contractDO.getSignDate(),1));
            }
            if (contractDO.getEndDate() != null) {
                contractQuery.setEndDateStart(DateKit.getBeforeDay(contractDO.getEndDate(),-1));
                contractQuery.setEndDateEnd(DateKit.getBeforeDay(contractDO.getEndDate(),1));
            }
            if (contractDO.getEmpSignDate() != null) {
                contractQuery.setEmpSignDateStart(DateKit.getBeforeDay(contractDO.getEmpSignDate(),-1));
                contractQuery.setEmpSignDateEnd(DateKit.getBeforeDay(contractDO.getEmpSignDate(),1));
            }
            if (contractDO.getSignFinishDate() != null) {
                contractQuery.setSignFinishDateStart(DateKit.getBeforeDay(contractDO.getSignFinishDate(),-1));
                contractQuery.setSignFinishDateEnd(DateKit.getBeforeDay(contractDO.getSignFinishDate(),1));
            }
        }
        return contractQuery;
    }

    /**
     * 保存示例
     * @return ContractVO
     */
    public ContractVO saveContractExample(){
        ContractAddDTO addDTO = this.getContractAddDTO();
        ContractVO contractVO = contractService.save(addDTO,buildTokenUser());
        return contractVO;
    }


}

